/**
 * Created by renzhiqiang on 17/4/23.
 */
import { staffsSearch } from './../../components/cor/publicFuns'
import pathToRegexp from "path-to-regexp";
import * as usersService from './../../services/list/add';

export default {
  namespace: 'add',
  state: {
    dataSource: [],
    foundStage: [],
    detailLoading: false,
    loading: false,
    detail: {},
    address: {}
  },
  reducers: {
    showDetailLoad(state, {payload: { detailLoading}}) {
      var detailLoading = !state.detailLoading;
      return {...state , detailLoading}
    },
    save(state, { payload: { data: list, pageParam, param, sorts, loading, filterBoxShowFlag} }) {
      const dataSource= list.data;
      const total = list.totalSize;
      return { ...state, dataSource, pageParam, total, param, sorts, loading, filterBoxShowFlag};
    },
    showLoading(state){
      var loading = true;
      return {...state, loading}
    },
    stageList(state, {preload: {foundStage}}){
      return {...state, foundStage}
    },
  },
  effects: {
    *init({},{put}){
      yield put({
        type: 'initData'
      });
    },
    *stage({}, { call, put, select}) {
      const data = yield call(staffsSearch, 'fund_type');
      debugger;
      let tmp = data.data.data.map(x => {
        return {text: x.name, value: x.id}
      });
      yield put({
        type: 'stageList',
        preload: {
          foundStage: tmp
        }
      });
    },
    *add({payload},{call, put, select}){
        var cons =yield select(state=> {
          return state.home;
        });
      console.info(cons);
        var data = yield call(usersService.add, payload);
        if (data.data.msg != "OK"){
          history.go(-1);
        }
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        const match = pathToRegexp('/add/:id').exec(pathname);
        if (match) {
          dispatch({
            type:'stage'
          });
        };
      });
    },
  },
};
